import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getUserInfoApi } from '@/api/UserApi.ts'

export const useUserStore = defineStore(
  'userStore',
  () => {
    const loginUser = ref<any>({
      id: '',
      username: '',
      nickname: '',
    })

    /**
     * 获取登录用户信息
     */
    async function fetchLoginUserInfo() {
      const res = await getUserInfoApi()
      if (res.data.code === 0 && res.data.data) {
        loginUser.value = res.data.data
      }
    }

    /**
     * 设置登录用户
     */

    function setLoginUser(newUserInfo: any) {
      loginUser.value = newUserInfo
    }

    return {
      loginUser,
      fetchLoginUserInfo,
      setLoginUser,
    }
  },
  // {
  //   persist: true,
  // },
)
